﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class FuJuanBars : Form
    {
        cMethods.DBHelper db;
        public FuJuanBars()
        {
            InitializeComponent();
            db = new cMethods.DBHelper();
        }

        private void FuJuanBars_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "选择复卷条码";
                FuJuanInfo();
                createTable();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void FuJuanInfo()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("select barCode,proCode,proName,spec from V_BarCode_Info");
            sb.Append(" where status='Y' and barStatus='need-fj'");
            if (!string.IsNullOrWhiteSpace(tbProCode.Text))
            {
                sb.Append(" and proCode like '%"+ tbProCode.Text +"%'");
            }
            if (!string.IsNullOrWhiteSpace(tbProName.Text))
            {
                sb.Append(" and proName like '%" + tbProName.Text + "%'");
            }
            if (!string.IsNullOrWhiteSpace(tbSpec.Text))
            {
                sb.Append(" and spec like '%" + tbSpec.Text + "%'");
            }
            var table = db.getData(sb.ToString(), "");
            this.dvList.DataSource = table;
            this.dvList.AutoGenerateColumns = false;
        }

        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                FuJuanInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private DataTable cTable = new DataTable();
       /// <summary>
       /// 创建临时表
       /// </summary>
        private void createTable()
        {
            if (cTable.Columns.Count < 1)
            {
                cTable.Columns.Add("barCode");
                cTable.Columns.Add("proCode");
                cTable.Columns.Add("proName");
                cTable.Columns.Add("spec");
            }
        }
        List<string> rList = new List<string>();
        private void dvList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string barCode = this.dvList.CurrentRow.Cells["barCode"].Value.ToString();
                if (!rList.Contains(barCode))
                {

                    DataRow r = cTable.NewRow();
                    r["barCode"] = barCode;
                    r["proCode"] = this.dvList.CurrentRow.Cells["proCode"].Value.ToString();
                    r["proName"] = this.dvList.CurrentRow.Cells["proName"].Value.ToString();
                    r["spec"] = this.dvList.CurrentRow.Cells["spec"].Value.ToString();
                    cTable.Rows.Add(r);
                    this.dvCList.DataSource = cTable;
                    this.dvCList.AutoGenerateColumns = false;
                    rList.Add(barCode);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dvCList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string barCode = this.dvCList.CurrentRow.Cells["sbarCode"].Value.ToString();
                for (int i = 0; i < rList.Count; i++)
                {
                    if (rList[i] == barCode)
                    {
                        rList.Remove(rList[i]); break;
                    }
                }
                for (int i = 0; i < cTable.Rows.Count; i++)
                {
                    if (cTable.Rows[i]["barCode"].ToString() == barCode)
                    {
                        cTable.Rows[i].Delete();
                    }
                }
                this.dvCList.DataSource = cTable;
                this.dvCList.AutoGenerateColumns = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSure_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                CommonMethods.SinglePar = "";
                for (int i = 0; i < cTable.Rows.Count; i++)
                {
                    CommonMethods.SinglePar = CommonMethods.SinglePar +cTable.Rows[i]["barCode"].ToString() + ",";
                }
                CommonMethods.SinglePar = CommonMethods.SinglePar.TrimEnd(',');
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
